﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Admin.Web.Controllers;
using YiSha.Business.YiDongYuan;
using YiSha.Entity.YiDongYuan;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Admin.Web.Areas.YiDongYuan.Controllers
{
    [Area("YiDongYuan")]
    public class GpsController : BaseController
    {
        [HttpGet]
        public ActionResult ShowFromTask([FromQuery] long id)
        {
            var detectionTask = new DetectionTaskBLL().GetEntity(id).Result;
            var detectionInfo = new DetectionInfoBLL().GetEntity(detectionTask.Data.DetectionInfoId.Value).Result;
            return Redirect("/YiDongYuan/Gps/Show?phone=" + detectionInfo.Data.GpsNo);
        }

        [HttpGet]
        public ActionResult Show([FromQuery] string phone)
        {
            ViewBag.phone = phone;
            return View();
        }

        [HttpGet]
        public ActionResult GetPositionList([FromQuery] string phone)
        {
            List<GpsPosition> list = new List<GpsPosition>();
            string url = GlobalContext.SystemConfig.HzxFindGpsUrl + "&sn=" + phone;
            var code = HttpHelper.HttpGet(url);
            var dto = code.ToObject<Hzx_ResultDTO>();
            if (dto.code == 200 && dto.data != null)
            {
                var nrGpsList = dto.data.ToString().ToList<Hzx_nr_gpspoint>();
                if (nrGpsList != null && nrGpsList.Count > 0)
                {
                    nrGpsList.ForEach(item =>
                    {
                        list.Add(new GpsPosition()
                        {
                            createDate = item.collect_Time,
                            direction = item.direction,
                            height = item.elevation,
                            latitude = item.lat,
                            longitude = item.lng,
                            phone = item.terminalSn,
                            speed = item.speed,
                            alarm = item.warn_mark,
                            status = item.status
                        });
                    });
                }
            }
            TData<List<GpsPosition>> result = new TData<List<GpsPosition>>()
            {
                Tag = list.Count > 0 ? 1 : 0,
                Data = list,
                Message = list.Count > 0 ? "" : "没有找到数据"
            };
            return Json(result);
        }
    }

    public class Hzx_ResultDTO
    {
        public int code { get; set; }
        public string msg { get; set; }
        public object data { get; set; }
    }

    public class Hzx_nr_gpspoint
    {
        /// <summary>
        /// 海拔
        /// </summary>
        public string elevation { get; set; }

        public string lng { get; set; }
        public DateTime collect_Time { get; set; }
        public string terminalSn { get; set; }
        public string speed { get; set; }
        public string lat { get; set; }
        public string direction { get; set; }
        public string warn_mark { get; set; }
        public string status { get; set; }
    }
}
